package com.example.wanxian.work_clock;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Wanxian on 2018/3/30.
 */

public class MyView extends View {
    private Paint paint =new Paint();
    private int hradius = 100;
    private int mradius = 150;
    private int sradius = 180;

    private float hour;
    private int minute;
    private int second;

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try{
                        Thread.sleep(1000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    getNowTime();
                    postInvalidate();
                }
            }
        }).start();
    }

    private void getNowTime() {
        Time time = new Time();
        time.setToNow();
        int h=time.hour;

        minute=time.minute;
        second=time.second;
        hour=h+minute*1.0f/60;
    }

    private void initPaint() {
        paint.setAntiAlias(true);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //paint.setStrokeWidth(5);
        paint.setColor(Color.BLACK);
        // paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(400,400,200,paint);
//假设现在是6点钟
        // float hour=12f;
        float hx=(float) (400+hradius * Math.sin(hour *30.0/180 *Math.PI));
        float hy=(float) (400-hradius * Math.cos(hour*30.0/180 *Math.PI));
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(10);
        canvas.drawLine(400,400,hx,hy,paint);


        float mx=(float) (400+mradius * Math.sin(minute*6.0/180 *Math.PI));
        float my=(float) (400-mradius * Math.cos(minute*6.0/180 *Math.PI));
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(8);
        canvas.drawLine(400,400,mx,my,paint);


        float sx=(float) (400+sradius * Math.sin(second*6.0/180 *Math.PI));
        float sy=(float) ((400-sradius * Math.cos(second*6.0/180 *Math.PI)));
        paint.setColor(Color.RED);
        paint.setStrokeWidth(5);
        canvas.drawLine(400,400,sx,sy,paint);


        super.onDraw(canvas);
    }
}

